home *** CD-ROM | disk | FTP | other *** search
/ Java for 3D & VRML Worlds / Java for 3d and VRML Worlds.iso / examples / chap05 / MuClient.class (.txt) < prev    next >
Encoding:
Java Class File  |  1996-10-06  |  3.7 KB  |  129 lines

  1. import java.io.DataInputStream;
  2. import java.io.DataOutputStream;
  3. import java.io.IOException;
  4. import java.net.Socket;
  5. import java.net.UnknownHostException;
  6. import vrml.BaseNode;
  7. import vrml.Browser;
  8. import vrml.Event;
  9. import vrml.field.ConstSFBool;
  10. import vrml.field.ConstSFRotation;
  11. import vrml.field.ConstSFVec3f;
  12. import vrml.field.SFNode;
  13. import vrml.field.SFRotation;
  14. import vrml.field.SFVec3f;
  15. import vrml.node.Node;
  16. import vrml.node.Script;
  17.  
  18. public class MuClient extends Script {
  19.    public static final String HOST = "localhost";
  20.    public static final int AVATARS = 3;
  21.    Socket socket;
  22.    // $FF: renamed from: in java.io.DataInputStream
  23.    DataInputStream field_0;
  24.    DataOutputStream out;
  25.    // $FF: renamed from: b vrml.Browser
  26.    Browser field_1;
  27.    SFVec3f[] avatarPosition = new SFVec3f[3];
  28.    SFRotation[] avatarRotation = new SFRotation[3];
  29.    float[] coord;
  30.    float[] rotation;
  31.    // $FF: renamed from: id int
  32.    int field_2;
  33.    boolean connected = false;
  34.  
  35.    public void initialize() {
  36.       this.field_1 = ((BaseNode)this).getBrowser();
  37.       Object var1 = null;
  38.  
  39.       for(int var2 = 0; var2 < 3; ++var2) {
  40.          try {
  41.             Node var4 = (Node)((SFNode)((Script)this).getField("avatar" + var2)).getValue();
  42.             this.avatarPosition[var2] = (SFVec3f)var4.getExposedField("translation");
  43.             this.avatarRotation[var2] = (SFRotation)var4.getExposedField("rotation");
  44.          } catch (Exception var3) {
  45.             this.field_1.setDescription("can not get avatar");
  46.             return;
  47.          }
  48.       }
  49.  
  50.       this.coord = new float[3];
  51.       this.rotation = new float[4];
  52.    }
  53.  
  54.    public void processEvent(Event var1) {
  55.       if (var1.getName().equals("entered")) {
  56.          ConstSFBool var2 = (ConstSFBool)var1.getValue();
  57.          if (var2.getValue() && !this.connected) {
  58.             try {
  59.                this.socket = new Socket("localhost", 4130);
  60.                this.field_0 = new DataInputStream(this.socket.getInputStream());
  61.                this.out = new DataOutputStream(this.socket.getOutputStream());
  62.                this.field_2 = this.field_0.readInt();
  63.                new MuReceiver(this.field_0, this);
  64.                this.connected = true;
  65.                return;
  66.             } catch (UnknownHostException var3) {
  67.                this.field_1.setDescription("Unknown host: localhost");
  68.                return;
  69.             } catch (Exception var4) {
  70.                this.field_1.setDescription("Connection error");
  71.                return;
  72.             }
  73.          }
  74.       } else {
  75.          if (var1.getName().equals("myPosition")) {
  76.             ((ConstSFVec3f)var1.getValue()).getValue(this.coord);
  77.  
  78.             try {
  79.                this.out.writeInt(this.field_2);
  80.                this.out.writeInt(1);
  81.                this.out.writeFloat(this.coord[0]);
  82.                this.out.writeFloat(this.coord[1]);
  83.                this.out.writeFloat(this.coord[2]);
  84.                return;
  85.             } catch (IOException var5) {
  86.                this.field_1.setDescription("Fail to send position to server:  " + var5);
  87.                return;
  88.             }
  89.          }
  90.  
  91.          if (var1.getName().equals("myOrientation")) {
  92.             ((ConstSFRotation)var1.getValue()).getValue(this.rotation);
  93.  
  94.             try {
  95.                this.out.writeInt(this.field_2);
  96.                this.out.writeInt(2);
  97.                this.out.writeFloat(this.rotation[0]);
  98.                this.out.writeFloat(this.rotation[1]);
  99.                this.out.writeFloat(this.rotation[2]);
  100.                this.out.writeFloat(this.rotation[3]);
  101.                return;
  102.             } catch (IOException var6) {
  103.                this.field_1.setDescription("Fail to send orientation to server:  " + var6);
  104.                return;
  105.             }
  106.          }
  107.       }
  108.  
  109.    }
  110.  
  111.    public void updatePosition(int var1, float var2, float var3, float var4) {
  112.       this.avatarPosition[var1].setValue(var2, var3, var4);
  113.    }
  114.  
  115.    public void updateOrientation(int var1, float var2, float var3, float var4, float var5) {
  116.       this.avatarRotation[var1].setValue(var2, var3, var4, var5);
  117.    }
  118.  
  119.    public void shutdown() {
  120.       try {
  121.          this.out.close();
  122.          this.field_0.close();
  123.          this.socket.close();
  124.       } catch (Exception var1) {
  125.          this.field_1.setDescription("Connection close error");
  126.       }
  127.    }
  128. }
  129.